package stream;

/**
 *
 * @author Simão Paulo Rato Alves Reis
 */
public class ByteCipher {

    public static void decipher(byte key, byte[] data) {
        int length;
        for (int index = 0; index < data.length; index++) {
            data[index] ^= key;
        }
        length = data.length - 1;
        for (int index = 0; index < length; index++) {
            for (int jndex = index + 1; jndex < data.length; jndex++) {
                data[jndex] ^= data[index];
            }
        }
    }

    public static void cipher(byte key, byte[] data) {
        byte tmpkey;
        for (int index = data.length - 2; index >= 0; index--) {
            for (int jndex = index + 1; jndex < data.length; jndex++) {
                tmpkey = data[index];
                tmpkey ^= data[jndex];
                data[jndex] ^= tmpkey;
                data[jndex] ^= tmpkey;
                tmpkey ^= data[jndex];
                data[jndex] ^= tmpkey;
            }
        }
        for (int index = 0; index < data.length; index++) {
            tmpkey = key;
            tmpkey ^= data[index];
            data[index] ^= tmpkey;
            data[index] ^= tmpkey;
            tmpkey ^= data[index];
            data[index] ^= tmpkey;
        }
    }
}
